package server;

import org.dom4j.Element;

import java.util.HashMap;

/**
 * @author: wudong
 * @create: 2021-07-27 14:56
 **/
public abstract class BaseContainer implements Container {
    protected String name;
    protected Element element;
    protected Container parent;
    protected final HashMap<String, Container> children = new HashMap<>();

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Container getParent() {
        return parent;
    }

    @Override
    public void setParent(Container container) {
        this.parent = parent;
    }

    @Override
    public void addChild(Container child) {
        children.put(child.getName(),child);
    }

    @Override
    public Container findChild(String name) {
        return children.get(name);
    }

    @Override
    public Container[] findChildren() {
        return children.values().toArray(new Container[0]);
    }

    @Override
    public void removeChild(Container child) {
        children.remove(child.getName());
    }

    @Override
    public Element getElement() {
        return element;
    }

    @Override
    public void setElement(Element element) {
        this.element = element;
    }
}
